package client;

import java.io.IOException;
import java.net.InetAddress;

public class UDPClient implements Runnable {
	private static final int TIMEOUT = 3000; // Resend timeout (ms)
	private static final int MAXTRIES = 5; // Max retransmissions
	
	private InetAddress serverAddress;
	private int port;
	
	public UDPClient(String serverAddress, int port) throws IOException {
		this.serverAddress = InetAddress.getByName(serverAddress); //Server address
		this.port = port;
	}
	
	public void run() {
//		DatagramSocket socket = new DatagramSocket();
//		socket.setSoTimeout(TIMEOUT); //Maximum receive blocking time (ms)
//		
//		DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length, serverAddress, servPort); //Sending packet

//		DatagramPacket receivePacket = new DatagramPacket(new byte[bytesToSend.length], bytesToSend.length); //Receiving packet
//		
//		int tries = 0;
//		boolean receivedResponse = false;
//		
//		do {
//			socket.send(sendPacket); // Send the echo string
//			try {
//				socket.receive(receivePacket); // Attempt echo reply reception
//				
//				if(!receivePacket.getAddress().equals(serverAddress)) { // Check source
//					throw new IOException("Received packet from an unknown souce");
//				}
//				receivedResponse = true;
//			} catch (InterruptedIOException e) { //We did not get anything
//				tries++;
//				System.out.println("Timed out, "+ (MAXTRIES - tries) + " more tries...");
//			}
//		} while ((!receivedResponse) && (tries < MAXTRIES));
//		
//		if (receivedResponse) {
//			System.out.println("Received: " + new String(receivePacket.getData()));
//		} else {
//			System.out.println("No response -- giving up.");
//		}
//		socket.close();
//		
	}
}
